package com.shujia.flink.core

//导入隐式转换
import org.apache.flink.streaming.api.scala._

object Demo1StreamWordCOunt {
  def main(args: Array[String]): Unit = {


    /**
      * 创建flink环境
      *
      */

    val env: StreamExecutionEnvironment = StreamExecutionEnvironment.getExecutionEnvironment

    //设置并行的
    //并行的由数据量
    //env.setParallelism(5)

    //读取socket 构建DS
    //nc -lk 8888
    val lineDS: DataStream[String] = env.socketTextStream("master", 8888)


    //1、将单词拆分
    val wordDS: DataStream[String] = lineDS.flatMap(_.split(","))


    //2、转换成kv格式
    val kvDS: DataStream[(String, Int)] = wordDS.map((_, 1))


    //3、安装key进行分组， 底层也是hash分区
    val keyByDS: KeyedStream[(String, Int), String] = kvDS.keyBy(_._1)

    //4、对value进行聚合
    val countDS: DataStream[(String, Int)] = keyByDS.sum(1)


    //打印结果
    countDS.print()


    ///启动flink程序
    env.execute()

  }

}
